const n = require("../../utils/request");

o = require("../../utils/storage")
Page({
  data: {
    formData: {
      name: '',
      gender: 1,
      age: '',
      identity_number: '',
      examineType: 0,
      phone: '',
      unit_name: '',
      depart: '',
      duty: '',
      workage: '',
      dangerCourse: '',
      workTimest: '2022-09-01',
      workTimeed: '2022-09-01',
      protectiveMeasures: '',
      diseaseName: '',
      diagnosisDate: '2022-09-01',
      iscured: '',
      fallzey: 0,
      fallyyxel:0,
      fallerming:0,
      fallbzs:0,
      fallheardbad:0,
      fallebln:0,
      issmoke:0,
      daynummoke:"",
      mokeTimest: '2022-09-01',
      isdayHeadset:0,
      isnoisy:0,
      sleepisHeadset:0,
      isknowbadHeadset:0,
      iscanhelpHeadset:0,
      isdrink:0,
      isfamilyHeadset:0,
      signname:"",
      diseaseList:[
        {
          diseaseName: '',
          diseaseUnitName: '',
          diagnosisDate: '20222-09-01',
          iscured: 1
        }
      ]
      // hbp: '',
      // dm: '',
      // cvsd: '',
      // uri: '',
      // noisyWorkTime: '',
      // hearingDamage: '',
      // other: ''
    },
    startDate: '1900-01-01',
    genderLabels: ['未知', '男', '女'],
    genderValues: [0, 1, 2],
    examineLabels: ['入职', '在职', '离职'],
    examineValuer:[0,1,2]
  },

  bindPickerChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`formData.${field}`]: value
    });
  },

  bindInputChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`formData.${field}`]: value
    });
  },
  bindInputDiseaseListChange(e) {
    const { index, field } = e.currentTarget.dataset;
    let diseaseList = this.data.formData.diseaseList;
    diseaseList[index][field] = e.detail.value;
    this.setData({
      [`formData.diseaseList`]: diseaseList
    });
  },

  bindRadioChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`formData.${field}`]: parseInt(value)
    });
  },
  addDisease() {
    let diseaseList = this.data.formData.diseaseList;
    diseaseList.push({
      diseaseName: '',
      diseaseUnitName: '',
      diagnosisDate: '20222-09-01',
      iscured: 1
    });
    this.setData({
      [`formData.diseaseList`]: diseaseList
    });
  },

  formSubmit(e) {
    console.log('Form submit data:', this.data.formData);
    let formData =  this.data.formData;
    formData.diseaseList = JSON.stringify(formData.diseaseList);
    n.post1('/wechat/home/crmTjActform', this.data.formData).then(function(e) {
      if(e.code ==1){
       
      }else {
          wx.showToast({
              title:e.msg,
              icon:'none',
              duration:2000
          })
      }
  })
  },

  formReset(e) {
    this.setData({
      formData: {
        name: '',
        gender: 1,
        age: '',
        identity_number: '',
        examineType: 0,
        phone: '',
        unit_name: '',
        depart: '',
        duty: '',
        workage: '',
        dangerCourse: '',
        workTimest: '2022-09-01',
        workTimeed: '2022-09-01',
        protectiveMeasures: '',
        diseaseName: '',
        diagnosisDate: '20222-09-01',
        iscured: '',
        fallzey: 0,
        fallyyxel:0,
        fallerming:0,
        fallbzs:0,
        fallheardbad:0,
        fallebln:0,
        issmoke:0,
        daynummoke:"",
        mokeTimest:"20222-09-01",
        isdayHeadset:0,
        isnoisy:0,
        sleepisHeadset:0,
        isknowbadHeadset:0,
        iscanhelpHeadset:0,
        isdrink:0,
        isfamilyHeadset:0,
        signname:"",
        diseaseList:[
          {
            diseaseName: '',
            diseaseUnitName: '',
            diagnosisDate: '20222-09-01',
            iscured: 1
          }
        ]
      }
    });
  }
});
